package com.piece.admin.controller.monitor;

import com.piece.admin.controller.ControllerSupport;
import com.piece.core.framework.annotation.permission.AclPermission;
import com.piece.core.framework.annotation.permission.SubmitPermission;
import com.piece.core.framework.constant.ExceptionConstants;
import com.piece.core.framework.constant.FrameWorkConstants;
import com.piece.core.log.enums.MessageLogType;
import com.piece.core.mongo.service.*;
import com.piece.core.framework.support.bootstrap.DataTable;
import com.piece.core.framework.support.bootstrap.QueryTable;
import com.piece.core.framework.support.response.AjaxResponse;
import com.piece.core.framework.util.basic.I18nUtil;
import com.piece.core.framework.util.string.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

@RestController
@Api(tags = {"系统日志管理"})
@RequestMapping("/api/system/log")
public class SysLogController extends ControllerSupport {

    @Resource
    private MongoLoginLogService mongoLoginLogService;

    @Resource
    private MongoAuditLogService mongoAuditLogService;

    @Resource
    private MongoBeanPostLogService mongoBeanPostLogService;

    @Resource
    private MongoMessageLogService mongoMessageLogService;

    @Resource
    private MongoJobLogService mongoJobLogService;

    @PostMapping("/login/table")
    @AclPermission(code = "log:login:view")
    @ApiOperation(value = "查询登陆日志分页数据")
    @ApiImplicitParam(value = "查询条件", dataTypeClass = QueryTable.class, type = "body")
    public AjaxResponse loginTable() {
        QueryTable queryTable = initTable();
        DataTable dataTable = mongoLoginLogService.findPage(queryTable);
        return success(dataTable);
    }

    @PostMapping("/audit/table")
    @AclPermission(code = "log:audit:view")
    @ApiOperation(value = "查询操作日志分页数据")
    @ApiImplicitParam(value = "查询条件", dataTypeClass = QueryTable.class, type = "body")
    public AjaxResponse auditTable() {
        QueryTable queryTable = initTable();
        DataTable dataTable = mongoAuditLogService.findPage(queryTable);
        return success(dataTable);
    }

    @PostMapping("/beanpost/table")
    @AclPermission(code = "log:audit:view")
    @ApiOperation(value = "查询数据表变更操作日志分页数据")
    @ApiImplicitParam(value = "查询条件", dataTypeClass = QueryTable.class, type = "body")
    public AjaxResponse beanPostTable() {
        QueryTable queryTable = initTable();
        DataTable dataTable = mongoBeanPostLogService.findPage(queryTable);
        return success(dataTable);
    }

    @PostMapping("/quartz/table")
    @AclPermission(code = "log:quartz:view")
    @ApiOperation(value = "查询定时任务日志分页数据")
    @ApiImplicitParam(value = "查询条件", dataTypeClass = QueryTable.class, type = "body")
    public AjaxResponse quartzTable() {
        QueryTable queryTable = initTable();
        DataTable dataTable = mongoJobLogService.findPage(queryTable);
        return success(dataTable);
    }

    @PostMapping("/message/table")
    @AclPermission(code = "log:message:view")
    @ApiOperation(value = "查询消息日志分页数据")
    @ApiImplicitParam(value = "查询条件", dataTypeClass = QueryTable.class, type = "body")
    public AjaxResponse messageTable() {
        QueryTable queryTable = initTable();
        DataTable dataTable = mongoMessageLogService.findPage(queryTable);
        return success(dataTable);
    }

    @SubmitPermission
    @PostMapping("/login/delete")
    @AclPermission(code = "log:login:delete")
    @ApiOperation(value = "删除登陆日志")
    @ApiImplicitParam(name = "ids", value = "登录日志ID集合")
    public AjaxResponse deleteLogin() {
        String ids = getRequest().getParameter("ids");
        if (StringUtil.isNotEmpty(ids)) {
            String[] idArr = ids.split(FrameWorkConstants.SPLIT_PARAM);
            for (String id : idArr) {
                mongoLoginLogService.delete(id);
            }
        } else {
            mongoLoginLogService.deleteAll();
        }
        return success(I18nUtil.message(ExceptionConstants.DELETE_SUCCESS), null);
    }

    @SubmitPermission
    @PostMapping("/audit/delete")
    @AclPermission(code = "log:audit:delete")
    @ApiOperation(value = "删除操作日志")
    @ApiImplicitParam(name = "ids", value = "操作日志ID集合")
    public AjaxResponse deleteOpera() {
        String ids = getRequest().getParameter("ids");
        if (StringUtil.isNotEmpty(ids)) {
            String[] idArr = ids.split(FrameWorkConstants.SPLIT_PARAM);
            for (String id : idArr) {
                mongoAuditLogService.delete(id);
            }
        } else {
            mongoAuditLogService.deleteAll();
        }
        return success(I18nUtil.message(ExceptionConstants.DELETE_SUCCESS), null);
    }

    @SubmitPermission
    @PostMapping("/beanpost/delete")
    @AclPermission(code = "log:audit:delete")
    @ApiOperation(value = "删除数据表变更操作日志")
    @ApiImplicitParam(name = "ids", value = "操作日志ID集合")
    public AjaxResponse deleteBeanPost() {
        String ids = getRequest().getParameter("ids");
        if (StringUtil.isNotEmpty(ids)) {
            String[] idArr = ids.split(FrameWorkConstants.SPLIT_PARAM);
            for (String id : idArr) {
                mongoBeanPostLogService.delete(id);
            }
        } else {
            mongoBeanPostLogService.deleteAll();
        }
        return success(I18nUtil.message(ExceptionConstants.DELETE_SUCCESS), null);
    }

    @PostMapping("/quartz/delete")
    @AclPermission(code = "log:quartz:delete")
    @ApiOperation(value = "删除定时任务日志")
    @ApiImplicitParam(name = "ids", value = "定时任务日志ID集合")
    public AjaxResponse deleteQuartz() {
        String ids = getRequest().getParameter("ids");
        if (StringUtil.isNotEmpty(ids)) {
            String[] idArr = ids.split(FrameWorkConstants.SPLIT_PARAM);
            for (String id : idArr) {
                mongoJobLogService.delete(id);
            }
        } else {
            mongoJobLogService.deleteAll();
        }
        return success(I18nUtil.message(ExceptionConstants.DELETE_SUCCESS), null);
    }

    @SubmitPermission
    @PostMapping("/message/delete")
    @AclPermission(code = "log:message:delete")
    @ApiOperation(value = "删除消息日志")
    @ApiImplicitParam(name = "ids", value = "消息日志ID集合")
    public AjaxResponse deleteMessage() {
        String ids = getRequest().getParameter("ids");
        if (StringUtil.isNotEmpty(ids)) {
            String[] idArr = ids.split(FrameWorkConstants.SPLIT_PARAM);
            for (String id : idArr) {
                mongoMessageLogService.delete(id);
            }
        } else {
            mongoMessageLogService.deleteAll(new String[]{MessageLogType.NOTICE.getType() + "", MessageLogType.MESSAGE.getType() + ""});
        }
        return success(I18nUtil.message(ExceptionConstants.DELETE_SUCCESS), null);
    }
}
